import { Reducer, Effect } from 'umi';
import { apiGetDva } from '@/server';

export interface CartModelState {
  tag: String;
  list: [];
}

export interface TagModelType {
  namespace: 'tag';
  state: CartModelState;
  effects: {
    fetchTags: Effect;
  };
  reducers: {
    setTagsList: Reducer<CartModelState>;
  };
}

const Tag: TagModelType = {
  namespace: 'tag', // 同构命名空间来调用 要保证唯一
  state: { tag: '', list: [] }, // 定义初始状态
  effects: {
    // 副作用 调用服务端接口 获取数据
    *fetchTags({ payload, callback }: any, { put, call }: any) {
      //  *  yield 异步变为同步效果
      const response = yield call(apiGetDva); // call(请求，参数)
      // 更新状态 调用reducers
      yield put({
        type: 'setTagsList', // 类似redux 中action的type
        payload: response,
      });
    },
  },
  reducers: {
    //  更新 state 中的数据
    setTagsList(state, action) {
      console.log(action.payload, state, '123213');
      // state 之前的状态 action
      return { list: action.payload.list, tag: action.payload.id };
    },
  },
};

export default Tag;
